<?php

/**
 * This is the model class for table "gadget".
 *
 * The followings are the available columns in table 'gadget':
 * @property integer $id
 * @property string $name
 * @property integer $categoryId
 * @property string $description
 * @property string $imageUrl
 * @property string $urlKey
 * @property integer $isActive
 *
 * The followings are the available model relations:
 * @property Gadgetcategory $category
 * @property Gadgetquote[] $gadgetquotes
 */
class Gadget extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Gadget the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'gadget';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, categoryId', 'required'),
			array('categoryId, isActive', 'numerical', 'integerOnly'=>true),
			array('name, imageUrl, urlKey', 'length', 'max'=>100),
			array('description', 'length', 'max'=>255),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, categoryId, description, imageUrl, urlKey, isActive', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'Gadgetcategory', 'categoryId'),
			'gadgetquotes' => array(self::HAS_MANY, 'Gadgetquote', 'gadgetId'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Name',
			'categoryId' => 'Category',
			'description' => 'Description',
			'imageUrl' => 'Image Url',
			'urlKey' => 'Url Key',
			'isActive' => 'Is Active',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('categoryId',$this->categoryId);
		$criteria->compare('description',$this->description,true);
		$criteria->compare('imageUrl',$this->imageUrl,true);
		$criteria->compare('urlKey',$this->urlKey,true);
		$criteria->compare('isActive',$this->isActive);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	public function getCategory(){
	
		if($this->categoryId!=null){
			
			$name = Gadgetcategory::model()->getCategory($this->categoryId);
			return $name;
		}else 	return null;
	}
	public function getStatus(){
		switch($this->isActive){
			case 0:
				return 'No';
			break;
			case 1:
				return 'Yes';
			break;
		}
	}
	public function searchProduct($device=null,$name=null,$isLimit=true){
	
		$criteria = new CDbCriteria;
		$criteria->together = true;
		
		if($name!=NULL){
			$criteria->addSearchCondition('t.name',$name,true,'OR');
		}
		$arrID = array();
		if($device!=NULL){
			$criteria->with = array('category');
			$categories = Gadgetcategory::model()->findByAttributes(array('urlKey'=>$device));
			if(!empty($categories)){
				$arrID[0] = $categories->id;
				if($categories->parentCategoryId!=null){
					
					// $categories = Gadgetcategory::model()->findByPk($categories->parentCategoryId);
					// if(!empty($categories)){
						// $arrID[] = $categories->id;
						// $categories = Gadgetcategory::model()->findAllByAttributes(array('parentCategoryId'=>$categories->id));
						// if(!empty($categories)){
							// foreach($categories as $key=>$value){
								// $arrID[0] = $value->id;
							// }
						// }
					// }
					
				}else{
					$categories = Gadgetcategory::model()->findAllByAttributes(array('parentCategoryId'=>$categories->id));
					if(!empty($categories)){
						foreach($categories as $key=>$value){
							$arrID[] = $value->id;
						}
					}
				}
				
				$arrID = array_unique($arrID);
				// sort($arrID);
				// dump($arrID,false);
				$criteria->addInCondition('category.id',$arrID,true,'OR');
			}
			
			
			
			$criteria->addSearchCondition('category.urlKey',$device,false,'OR');
		}
		// dump($criteria,false);
		$criteria->compare('t.isActive',1);
		$result = $this->findAll($criteria);
		$pages = null;
		if($isLimit){
			$pages				=	new CPagination(count($result));
			$pages->pageSize	=	100;
			$pages->applyLimit($criteria);
			$result = $this->findAll($criteria);
		}
		
		return array(
			'data' => $result,
			'pagination' => $pages,
			'count' => count($result),
		);
	}
	public function getBizFromAlias($alias=null){
		$model = $this->findByAttributes(array(
			'urlKey'=>$alias
		));
		return $model;
	}
}